package emulator.unittest.util;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import emulator.util.MemoryPtr;

public class MemoryPtrTest {

	private byte[] memory;
	private MemoryPtr ptr0;
	private MemoryPtr ptr1;
	private MemoryPtr ptr2;

	@Before
	public void setUp() throws Exception {
		memory = new byte[] { 0x01, (byte)0xF2, 0x03, (byte) 0xF4, 0x05 };
		ptr0 = new MemoryPtr(memory, 0);
		ptr1 = new MemoryPtr(memory, 1);
		ptr2 = new MemoryPtr(memory, 2);
	}

	@Test
	public void testGet() {
		byte[] bytes = new byte [3];
		ptr0.get(bytes);
		assertEquals(memory[0], bytes[0]);
		assertEquals(memory[1], bytes[1]);
		assertEquals(memory[2], bytes[2]);
		ptr1.get(bytes);
		assertEquals(memory[1], bytes[0]);
		assertEquals(memory[2], bytes[1]);
		assertEquals(memory[3], bytes[2]);
		ptr2.get(bytes);
		assertEquals(memory[2], bytes[0]);
		assertEquals(memory[3], bytes[1]);
		assertEquals(memory[4], bytes[2]);
	}

	@Test
	public void testGetWordOp() {
		assertEquals(0x03F2, ptr0.getWordOp());
		assertEquals(0xF403, ptr1.getWordOp());
		assertEquals(0x05F4, ptr2.getWordOp());
	}

	@Test
	public void testGetByteOp() {
		assertEquals(0xF2, ptr0.getByteOp());
		assertEquals(0x03, ptr1.getByteOp());
		assertEquals(0xF4, ptr2.getByteOp());
	}

	@Test
	public void testGetRelativeOp() {
		assertEquals(-12, ptr0.getRelativeOp());
		assertEquals(  6, ptr1.getRelativeOp());
		assertEquals( -8, ptr2.getRelativeOp());
	}

}
